package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Category;
import com.sky.entity.Employee;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServicempl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageselect(CategoryPageQueryDTO categoryPageQueryDTO) {
        //把page页面
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        //通过数据库中的name名字和type模糊查询数据
        Page<Category> page= categoryMapper.pagselect(categoryPageQueryDTO.getName(),categoryPageQueryDTO.getType());
        //几页
        long total = page.getTotal();
        //list集合的category的数据
        List<Category> result = page.getResult();
        return new  PageResult(total,result);

    }
    /**
     * 启动禁用
     * @param status
     * @param id
     */
    @Override
    public void enDi(int status, long id) {
        Category category = Category.builder().status(status).id(id).build();
        categoryMapper.update(category);
    }

    @Override
    public Category selectIdscate(Integer type) {
       Category category= categoryMapper.selectType(type);
        return category;
    }

    /**
     * 添加菜品
     * @param categoryDTO
     */
    @Override
    public void add(CategoryDTO categoryDTO) {
        //创造一个对象
        Category category = new Category();
        //这是将相同的成员变量放入另一个对象中
        BeanUtils.copyProperties(categoryDTO, category);
        //设置账号的状态，默认正常状态 1表示正常 0表示锁定
        category.setStatus(StatusConstant.ENABLE);
        //修改时间
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        //使用了本地线程,在登录检测中拿到了管理员的id
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
          categoryMapper.add(category);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        //创造一个对象
        Category category = new Category();
        //这是将相同的成员变量放入另一个对象中
        BeanUtils.copyProperties(categoryDTO, category);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
         categoryMapper.update(category);
    }

    @Override
    public void delete(Integer id) {

        categoryMapper.delete(id);
    }


}
